//
//  MineSetVC.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/27.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "MineSetVC.h"
#import "MineSetModel.h"
#import "MineSetTableCell.h"
#import "mineLoginMailVC.h"
#import "FileHandler.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface MineSetVC ()
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSMutableArray *moreArray;
@end

@implementation MineSetVC

-(void)viewWillAppear:(BOOL)animated{
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    self.userName = [defa objectForKey:@"userName"];
    [self getData];
    [self.myTabelView reloadData];
    if (self.userName  == nil) {

    }else{

    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;

    
    NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
    self.userName = [defa objectForKey:@"userName"];
    [self getData];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT - 60) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTabelView];
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    
    //注册
    UINib *nib = [UINib nibWithNibName:@"MineSetTableCell" bundle:[NSBundle mainBundle]];
    [self.myTabelView registerNib:nib forCellReuseIdentifier:@"reuse"];
    
    //返回按钮
    [self addChancelButton];
    
}

- (void)getData
{
    self.myArrayM = [NSMutableArray array];
    self.myArrayMA = [NSMutableArray array];
    self.myArrayMB = [NSMutableArray array];
    
    
     NSMutableArray *array = [NSMutableArray arrayWithObjects:@"账号", @"推送设置", @"清除缓存",@"意见反馈", @"评价穷游", @"分享穷游",@"关于穷游", nil];
    if (self.userName == nil) {
        self.moreArray = [NSMutableArray arrayWithObjects:@"登录", @"⇥",@"", @"", @"", @"", @"", nil];
    }else
    {
      self.moreArray = [NSMutableArray arrayWithObjects:self.userName, @"⇥",@"", @"", @"", @"", @"", nil];
    }
   
    for (int i = 0; i < 7; i++) {
        MineSetModel *model = [[MineSetModel alloc] init];
        model.title = [array objectAtIndex:i];
        model.photo = [NSString  stringWithFormat:@"setUp_%d",i+1];
        model.more = [self.moreArray objectAtIndex:i];
        if (i < 3) {
            [self.myArrayMA addObject:model];
        }else
        {
            [self.myArrayMB addObject:model];
        }
    }
    [self.myArrayM addObject:self.myArrayMA];
    [self.myArrayM addObject:self.myArrayMB];
}


- (void) addChancelButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(20, 30, 25, 25);
    [button setImage:[UIImage imageNamed:@"btn-fanhui64x64"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, 25, 100, 30)];
    label.text = @"设置";
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    
    UILabel *fgx = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, WIDTH, 1)];
    fgx.backgroundColor = [UIColor colorWithRed:220/ 255.0 green:220 / 255.0 blue:220 /255.0 alpha:1.0];
    [self.view addSubview:fgx];
}

//返回按钮点击方法
- (void)buttonAction:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

//分区的个数
-(NSInteger )numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.myArrayM.count;
}

//分区头高度
-(CGFloat )tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //背景颜色
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor whiteColor];

    //标题
        UILabel *text = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, WIDTH - 40 , 40)];
        [view addSubview:text];
        text.textColor = [UIColor blackColor];
        
        //文字定义
        if (section == 0) {
            text.text = @"个人设置";
        }else
        {
            text.text = @"应用相关";
        }
        return view;
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return self.myArrayMA.count;
    }
    return self.myArrayMB.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MineSetTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (indexPath.section == 0) {
        MineSetModel *model = [self.myArrayMA objectAtIndex:indexPath.row];
        cell.myModel = model;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    MineSetModel *model = [self.myArrayMB objectAtIndex:indexPath.row];
    cell.myModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//cell触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSUserDefaults *deta = [NSUserDefaults standardUserDefaults];
    if (self.userName  == nil) {
        mineLoginMailVC *loginMailVC = [[mineLoginMailVC alloc] init];
        [self presentViewController:loginMailVC animated:YES completion:^{
            
        }];
        
    } else{
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                
                [self addAlertControllerWithTitle:@"确定登出账号?" withMessage:@""];
                [deta removeObjectForKey:@"userName"];
                [deta removeObjectForKey:@"userPic"];
                [self getData];
                [self viewDidLoad];
            }
            if (indexPath.row == 2) {
                FileHandler *handler = [FileHandler new];
               float size =  [handler clearCache];
                [self showAlert:[NSString stringWithFormat:@"清除缓存成功,释放空间 %.2fM!",size]];
            }
        }
    }
}


//创建

-(void)addAlertControllerWithTitle:(NSString *)acTitle withMessage:(NSString *)message
{
    //取消按钮

    UIAlertController * alertC = [UIAlertController alertControllerWithTitle:acTitle message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertC addAction:cancelAction];
    
     UIAlertAction *defaltAction = [UIAlertAction actionWithTitle:@"登出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

         [self.myTabelView reloadData];
    }];
    
    [alertC addAction:defaltAction];
    
    //运行
    [self presentViewController:alertC animated:YES completion:^{
        
    }];
}


//刷新后显示对话框
- (void)timerFireMethod:(NSTimer*)theTimer  //弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert =NULL;
}

- (void)showAlert:(NSString *) _message{   //时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    
    [NSTimer scheduledTimerWithTimeInterval:3.0f
                                     target:self
                                   selector:@selector(timerFireMethod:)
                                   userInfo:promptAlert
                                    repeats:YES];
    [promptAlert show];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
